package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.InfluenceRadius;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.service.InfluenceRadiusService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/30 15:24
 */
public class InfluenceRadiusListener extends AnalysisEventListener<InfluenceRadius> {
    private InfluenceRadiusService influenceRadiusService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public InfluenceRadiusListener() {
    }

    public InfluenceRadiusListener(InfluenceRadiusService influenceRadiusService, PartitionBaseInfoService partitionBaseInfoService) {
        this.influenceRadiusService = influenceRadiusService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(InfluenceRadius influenceRadius, AnalysisContext analysisContext) {
        //计算平均值
        Integer average = CalculateUtils.average(influenceRadius.getInfluenceRadiusMin(), influenceRadius.getInfluenceRadiusMax());
        influenceRadius.setInfluenceRadiusAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",influenceRadius.getLandform()).eq("clay_thickness",influenceRadius.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        influenceRadius.setPartitionId(partitionBaseInfo.getId());
        influenceRadius.setPartitionCode(partitionBaseInfo.getPartitionCode());
        influenceRadius.setAreaCode(influenceRadius.getArea()+influenceRadius.getPartitionCode());
        UpdateWrapper<InfluenceRadius> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",influenceRadius.getAreaCode()).eq("aquifer_lithology",influenceRadius.getAquiferLithology());
        influenceRadiusService.saveOrUpdate(influenceRadius,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
